import 'package:flutter_yishop/config/Config.dart';
import 'package:flutter_yishop/constants/Constants.dart';
import 'package:sp_util/sp_util.dart';

///立即购买
class BuyServices {
  static final String id = '_id';
  static final String title = 'title';
  static final String price = 'price';
  static final String selectedAttr = 'selectedAttr';
  static final String count = 'count';
  static final String pic = 'pic';
  static final String checked = 'checked';

  static add(item) {
    //把对象转换为map
    item = BuyServices.formatCartData(item);
    List tempList = new List();
    tempList.add(item);
    SpUtil.putObjectList(Constants.BUY,tempList);

  }

  //过滤数据
  static formatCartData(item) {
    final Map data = Map();
    data[id] = item.sId;
    data[title] = item.title;
    data[price] = item.price;
    data[selectedAttr] = item.selectedAttr;
    data[count] = item.count;
    data[pic] = "${Config.url}${(item.pic).replaceAll("\\", "/")}";
    //是否选中
    data[checked] = true;
    return data;
  }

  //获取立即购买数据
  static List getBuyData(){
    List buyListData = [];
    List tempCheckOutData = [];
    try{
      buyListData = SpUtil.getObjectList(Constants.BUY);
    }catch(e){
      buyListData=[];
    }
    for(int i = 0;i<buyListData.length;i++){
        tempCheckOutData.add(buyListData[i]);
    }
    return tempCheckOutData;
  }


}
